/**

 */

(function($) { 
$.fn.touchobject = function(settings) {
	// ---------------------------------------------------------------------------
	// Setup	
	var config = {
		disabled: false,
		cancel: ':input,option',
		containment: false,
		handle: false,
		preventDefaultEvents: true
	};
	if (settings) $.extend(config, settings);
	var selector = this.selector;
	if (selector == '') return false;
	var object = null;
	var handle = '';
	var touch = {x1:-1,y1:-1,x2:-1,y2:-1};
	var handle_touch = null;
	var obj_touch = null;
	var handle_tid = -1;
	var obj_tid = -1;
	var handle_index = -1;
	var obj_index = -1;	
	var obj_pos = {x1:-1,y1:-1,x2:-1,y2:-1};
	var touch_cnt = 0;
	var prev_border = '';
	if (config.handle != ''){
		handle = config.handle;
	} else {
		handle = selector;
	}
	// ---------------------------------------------------------------------------
	// Secondary Events	
	function touchstart(e,ishandle){
		var ev = e.originalEvent;
		var t; 
		if (ishandle) {
			prev_border = object.css('border');
			object.css('border','2px solid red');
			t = ev.touches.item(0);
			handle_tid = t.identifier;
			wapp.log('Start Touch ' + handle_tid);
			touch.x1 = t.pageX;
			touch.y1 = t.pageY;	
			touch_cnt = 1;
			t= null;
		} else if (touch_cnt == 1) {
			t = ev.touches.item(1);
			obj_tid = t.identifier;
			wapp.log('Start Multi-Touch ' + obj_tid);
			touch.x2 = t.pageX;
			touch.y2 = t.pageY;					
			touch_cnt = 2;
			t= null;
			return false;
		}
	}
	function touchIdentIndex(ident,touches){
		var tobj;
		for (var i in touches){
			tobj = touches.item(i);
			if (tobj.identifier == ident) {
				return i;
			}
		}		
		return null;
	}
	function touchmove(e,ishandle){
		if (typeof ishandle == "undefined" || ishandle == null) {
			var ishandle = false;
		}		
		var ev = e.originalEvent;
		var pos = {x1:-1,y1:-1,x2:-1,y2:-1};
		var cpos = object.position();
		var ht;
		var ot;
		handle_index = touchIdentIndex(handle_tid,ev.touches);
		obj_index = touchIdentIndex(obj_tid,ev.touches);
		if (touch_cnt == 2 && handle_index != null && obj_index != null) {
			ht = ev.touches.item(handle_index);
			ot = ev.touches.item(obj_index);
			var hdl = touch.x1 - ht.pageX;
			var hdt = touch.y1 - ht.pageY;
			var odl = touch.x2 - ot.pageX;
			var odt = touch.y2 - ot.pageY;		
			// position
			pos.x1 = obj_pos.x1 - hdl;
			pos.y1 = obj_pos.y1 - hdt;
			if (cpos.left != pos.x1){
				object.css('left',pos.x1 + 'px');
			}				
			if (cpos.top != pos.y1){
				object.css('top',pos.y1 + 'px');
			}			
			// height width

			pos.x2 = obj_pos.x2 - odl;
			pos.y2 = obj_pos.y2 - odt;			
			if (object.width() != pos.x2){
				object.css('width',pos.x2 + 'px');
			}				
			if (object.height() != pos.y2){
				object.css('height',pos.y2 + 'px');
			}			
		} else if (touch_cnt == 1 && handle_index != null) {
			// object will be moved
			ht = ev.touches.item(handle_index);
			var hdl = touch.x1 - ht.pageX;
			var hdt = touch.y1 - ht.pageY;
			pos.x1 = obj_pos.x1 - hdl;
			pos.y1 = obj_pos.y1 - hdt;
			if (cpos.left != pos.x1){
				object.css('left',pos.x1 + 'px');
			}				
			if (cpos.top != pos.y1){
				object.css('top',pos.y1 + 'px');
			}
		}
		ht = null;
		return false;
	}
	function echo(msg){
		$(object).find('.app_window_title').html(msg);
		wapp.log(msg);
	}
	function touchend(e,ishandle){
		var ev = e.originalEvent;
		if (handle != selector && ishandle){
			$('body').unbind('touchstart',touchstart);
			$('body').unbind('touchmove',touchmove);
			$('body').unbind('touchend',touchend);
			$('body').unbind('touchcancel',touchend);
		}
		if (!ishandle) {
			obj_index = -1;
			obj_tid = -1;	
			touch_cnt = 1;
			return false;
		}
		if (ishandle) {
			handle_index = -1;
			handle_tid = -1;
			touch_cnt = 0;
			obj_index = -1;
			obj_tid = -1;	
			touch_cnt = 1;
			touch = {x1:-1,y1:-1,x2:-1,y2:-1};
			object.css('border',prev_border);
			wapp.log('End Touch');
		}			
	}
	// ---------------------------------------------------------------------------
	// Primary Events
	$('body').delegate(handle,'touchstart',function(e) {
		var ev = e.originalEvent;
		if (config.disabled) return true;
		if (ev.touches.length == 1) {			
			if (handle != selector){
				object = $(e.target).closest(selector);
				$('body').bind('touchstart',touchstart);
				$('body').bind('touchmove',touchmove);
				$('body').bind('touchend',touchend);
				$('body').bind('touchcancel',touchend);				
			} else {
				object = $(e.target);
			}
			if (object.length == 1){
				var p = object.position();
				obj_pos.x1 = p.left;
				obj_pos.y1 = p.top;
				obj_pos.x2 = object.width();
				obj_pos.y2 = object.height();
				touchstart(e,true);
			}
			// handle down
		} else if (ev.touches.length == 2) {
			if (handle == selector && touch_cnt == 1){
				return touchstart(e);
			}
		}
	}.bind(this));					
	$('body').delegate(handle,'touchmove',function(e) {
		return touchmove(e,true);
	});	
	$('body').delegate(handle,'touchend touchcancel',function(e) {
		return touchend(e,true);		
	});	
	// ---------------------------------------------------------------------------
	return this;
};
})(jQuery);
